var include_js_mapping =
{
    "dvs" : [ '<script src="${basePath}static/js/dvs.js" type="text/javascript"></script>'],

    "validation" : [ '<script src="${basePath}static/js/validation/jquery.validate.js" type="text/javascript"></script>',
        '<script src="${basePath}static/js/validation/additional-methods.min.js" type="text/javascript"></script>',
        '<script src="${basePath}static/js/validation/localization/messages_zh.js" type="text/javascript"></script>',
        '<link href="${basePath}static/js/validation/jquery.validate.custom.css" rel="stylesheet" type="text/css" />',
        '<script src="${basePath}static/js/validation/jquery.validate.custom.js" type="text/javascript"></script>',
        '<link href="${basePath}static/js/qtip/jquery.qtip.min.css" rel="stylesheet" type="text/css" />',
        '<script src="${basePath}static/js/qtip/jquery.qtip.min.js" type="text/javascript"></script>' ],

    "jquery" : [ '<script src="${basePath}static/js/jquery/jquery-1.12.1.min.js" type="text/javascript"></script>' ],

    "jquery_json" : [ '<script src="${basePath}static/js/jquery_json/jquery.json.min.js" type="text/javascript"></script>' ],
};

if (!Array.indexOf) {
    Array.prototype.indexOf = function(item) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == item) {
                return i;
            }
        }
        return -1;
    }
}

if (!Array.lastIndexOf) {
    Array.prototype.lastIndexOf = function(item) {
        for (var i = this.length; i >= 0; i--) {
            if (this[i] == item) {
                return this.length - i;
            }
        }
        return -1;
    }
}


var dvs = dvs || {};

dvs.includeCssJs = function(basePath, groups) {
    var ss = [];
    for (var i = 0; i < groups.length; i++) {
        var g = groups[i];
        if (!dvs.includeCssJsList) {
            dvs.includeCssJsList = [];
        }
        if (dvs.includeCssJsList.indexOf(g) == -1) {
            dvs.includeCssJsList.push(g);
        } else {
            // continue;
        }

        if (g.indexOf(".css") > 0) {
            ss.push('<link href="' + basePath + g + '" rel="stylesheet" type="text/css" />');
        } else if (g.indexOf(".js") > 0) {
            ss.push('<script src="' + basePath + g + '" type="text/javascript"></script>');
        } else {
            dvs.includeCssJsGroup(basePath, g, ss);
        }
    }
    document.write(ss.join(""));
};

dvs.includeCssJsGroup = function(basePath, g, ss) {
    var jsArray = include_js_mapping[g];
    if (jsArray && jsArray.length > 0) {
        for (var i = 0; i < jsArray.length; i++) {
            var js = jsArray[i];
            js = js.replace(/\${basePath}/g, basePath);
            ss.push(js);
        }
    } else {
        alert("dvs.includeCssJsGroup fail : " + g);
    }
};

(function() {
    var href = document.location.href;
    var search = document.location.search;
    var scripts = document.getElementsByTagName("script");
    for (var i = 0; i < scripts.length; i++) {
        var script = scripts[i];
        var basePath = "";
        var groups = [];
        var pos = script.src.indexOf("/static/js/include.js");
        if (pos != -1) {
            var contextPath = href.substring(0, pos);// http://localhost/contextPath
            var p = contextPath.indexOf("/", 8);
            if (p == -1) {
                dvs.contextPath = "";
            } else {
                dvs.contextPath = contextPath.substring(p);
            }

            var path = href.substring(pos + 1, href.lastIndexOf(search));
            var ss = path.split("/");
            var n = ss.length - 1;
            var j = 0;
            while (j < n) {
                basePath += "../";
                j++;
            }
            groups = script.src.substring(pos + 22).split(",");
        }

        if (groups.length > 0) {
            dvs.includeCssJs(basePath, groups);
        }
    }
})();